package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.HomeAdaverViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryPageResourceRes;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/11 10:53
 * description:
 */
public class HomeAdaverAdapter extends DelegateAdapter.Adapter<HomeAdaverAdapter.HomeAdaverHolder> {

    private Context mContext;
    private ArrayList<Integer> iconList = new ArrayList<>();


    private LayoutHelper mGridLayoutHelper;
    private QueryPageResourceRes viewData;
    private List<QueryPageResourceRes.ResourceListBean> resource_list;



    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int needHeader, int resource_id,int modularid);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    private BaseInterface baseInterface;

    public HomeAdaverAdapter(Context context, DefaultLayoutHelper adavweHelper) {
        mContext = context;

        mGridLayoutHelper = adavweHelper;

//        iconList.add(R.drawable.home_adaver_lc);
//        iconList.add(R.drawable.home_adaver_jzsh);
//        iconList.add(R.drawable.home_kljb);

    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.home_adaver_view;
    }

    @Override
    public HomeAdaverHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return HomeAdaverHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(HomeAdaverHolder holder, int position) {
        loadItemData(position, holder.binding);

    }

    private void loadItemData(final int position, HomeAdaverViewBinding binding) {
        if (resource_list != null) {
            String resource_url = resource_list.get(position).getResource_url();
            final ImageView homeAdaverIv = binding.homeAdaverIv;
            Glide.with(mContext)
                    .load(resource_url)
                    .into(homeAdaverIv);
            homeAdaverIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");

                    QueryPageResourceRes.ResourceListBean bean = resource_list.get(position);
                    int need_header = bean.getNeed_header();
                    String url = bean.getLink_url();
                    String sys_label = bean.getSys_label();
                    int resource_id = bean.getResource_id();
                    int modularid = bean.getModularId();
                    if (access_token == null) {
                        JumpUtils.jump(mContext, "login", 0);
                        return;
                    }
                    if (baseInterface != null) {
                        baseInterface.onClick(null, url, sys_label, need_header, resource_id,modularid);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == resource_list ? 0 : resource_list.size();
    }

    public void setViewData(QueryPageResourceRes viewData) {
        if (viewData != null) {
            this.viewData = viewData;
            resource_list = viewData.getResource_list();
            notifyDataSetChanged();
        }
    }

    public static class HomeAdaverHolder extends RecyclerView.ViewHolder {
        private HomeAdaverViewBinding binding;

        public static HomeAdaverHolder creat(ViewGroup parent) {
            HomeAdaverViewBinding binding = HomeAdaverViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new HomeAdaverHolder(binding);
        }

        public HomeAdaverHolder(HomeAdaverViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
